package com.lemon.boot.service.impl;

import com.lemon.boot.common.base.BaseServiceImpl;
import com.lemon.boot.common.entity.LoginUserDetail;
import com.lemon.boot.common.enums.StatusEnum;
import com.lemon.boot.common.utils.JwtUtil;
import com.lemon.boot.dao.NoticeDAO;
import com.lemon.boot.dto.NoticeCountDTO;
import com.lemon.boot.dto.NoticeDTO;
import com.lemon.boot.po.NoticePO;
import com.lemon.boot.service.INoticeService;
import com.lemon.boot.service.INoticeUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通知公告表 业务实现类
 *
 * @author Lemon
 * @since 2024-02-29
 */
@Log4j2
@Service
public class NoticeServiceImpl extends BaseServiceImpl<NoticeDAO, NoticePO, NoticeDTO> implements INoticeService {
    @Resource
    private INoticeUserService iNoticeUserService;

    @Override
    public List<NoticePO> listByMap(Map<String, Object> columnMap) {
        LoginUserDetail currentUser = JwtUtil.getCurrentUser();
        columnMap.put(StatusEnum.ENABLE.getColumn(), StatusEnum.ENABLE.getStatus());
        columnMap.put("userId", currentUser.getUserId());
        return this.baseMapper.getByCurrent(columnMap);
    }

    @Override
    public long getCountByUserId(Long userId, Integer isRead) {
        Map<String, Object> param = new HashMap<>();
        param.put(StatusEnum.ENABLE.getColumn(), StatusEnum.ENABLE.getStatus());
        param.put("userId", userId);
        param.put("isRead", isRead);
        return this.baseMapper.getCountByCurrent(param);
    }

    @Override
    public Map<Long, Long> getCountByUserId(List<Long> userIdList) {
        //1.判空
        if (userIdList.isEmpty()) {
            return new HashMap<>(0);
        }

        //2.userIdList 去重复
        List<Long> distinctUserIdList = userIdList.stream().distinct().collect(Collectors.toList());

        //3.封装请求参数
        Map<String, Object> param = new HashMap<>();
        param.put(StatusEnum.ENABLE.getColumn(), StatusEnum.ENABLE.getStatus());
        param.put("userIdList", distinctUserIdList);
        param.put("isRead", 0);

        //4.查询数据
        List<NoticeCountDTO> noticeCountList = this.baseMapper.getCountByUserId(param);

        //5.返回结果
        Map<Long, Long> result = new HashMap<>(distinctUserIdList.size());
        userIdList.forEach(userId -> {
            long count = 0L;
            if (!noticeCountList.isEmpty()) {
                List<NoticeCountDTO> userCount = noticeCountList.stream().filter(item -> item.getUserId().equals(userId)).collect(Collectors.toList());
                if (userCount.size() == 1) {
                    count = userCount.get(0).getCount();
                }
            }
            result.put(userId, count);
        });
        return result;
    }

    @Override
    @Transactional
    public String add(NoticePO noticePO) {
        //1.设置默认值后，执行添加
        noticePO.setStatus(StatusEnum.ENABLE.getStatus());
        super.add(noticePO);

        //2.添加 sys_notice_user
        iNoticeUserService.addByDeptIdList(noticePO.getId(), noticePO.getDeptIdList());

        return "发送成功！";
    }
}